package blockchain.block;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class Sha {
    // 计算SHA-256哈希
    public static String Sha256(String input) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");// 获取 SHA-256 算法的 MessageDigest 实例
            byte[] hash = digest.digest(input.getBytes());      // 将输入字符串转换为字节数组并进行哈希计算
            return Base64.getEncoder().encodeToString(hash);  // 将字节数组转换为 Base64 编码的字符串，方便显示
        } catch (NoSuchAlgorithmException e) { System.out.println(e.toString()); }
        return "";
    }
}
